<?php
/** Include PHPExcel */
require_once dirname(__FILE__) . '/../Lib/PHPExcel.php';
class AdminController extends AppController
{
    public $name = 'Admin';
    public $components = array('Session');
    //public $uses = array('tbl_admin_member', 'connection_log');
    public $uses = array('tbl_admin_member','tbl_log','tbl_account', 'tbl_character','tbl_connect_log','tbl_device_log','tbl_stage_log');

    public function lang_change($_lang)
    {
        CakeSession::write('Config.language', $_lang);
        $this->redirect(array('controller' => Admin, 'action' => 'login_index'));
    }

    public function login_index()
    {
        if(!empty($this->data['Admin']))
        {
            $admin_id = $this->data['Admin']['admin_id'];
            $admin_pass = $this->data['Admin']['admin_pass'];

            if(!$admin_info = $this->tbl_admin_member->admin_login($admin_id, $admin_pass))
            {
                $err_msg = __(LANG_ADMIN_ID_PASS_NOT);
                $this->Session->delete('login_session');

                $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>'Admin_login','operation_data'=>$admin_id,'operation_result'=>'Login Fail');
                $this->tbl_log->save($logdata);

            }
            else
            {
                print_r($admin_info);

                $this->Session->write('login_session.admin_auth', $admin_info['tbl_admin_member']['admin_auth']);
                $this->Session->write('login_session.admin_id', $admin_info['tbl_admin_member']['admin_id']);
                $this->Session->write('login_session.admin_pass', $admin_info['tbl_admin_member']['admin_pass']);
                $this->Session->write('login_session.admin_name', $admin_info['tbl_admin_member']['admin_name']);


                //로그 설정
                $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>'Admin_login','operation_data'=>$admin_info['tbl_admin_member']['admin_id'],'operation_result'=>'Login Success');
                $this->tbl_log->save($logdata);

                $this->redirect(array('controller' => 'Notifi', 'action' => 'all'));
            }
        }

        $this->set(compact('err_msg'));
    }

    public function logout_index()
    {
        $this->Session->delete('login_session');
        $this->redirect(array('controller' => 'Admin', 'action' => 'login_index',));
    }

    private function __admin_del($_admin_del_data)
    {
        foreach($_admin_del_data as $num => $null_check)
        {
            if($null_check == 0)
            {
                unset($_admin_del_data[$num]);
            }
            else
            {
                $admin_num = $_admin_del_data[$num];

                if($admin_info = $this->tbl_admin_member->get_admin_num($admin_num))
                {
                    $log_data = connectionLog(
                        ADMIN_DEL,
                        $_SESSION['login_session']['admin_id'],
                        $_SESSION['login_session']['admin_name'],
                        '',
                        $admin_info['tbl_admin_member']['admin_id'],
                        $this->request->clientIp()
                    );

                    $this->connection_log->create();
                    $this->connection_log->save($log_data);
                }

                $this->tbl_admin_member->delete($admin_num);
            }
        }
    }

    public function admin_list()
    {
        if(empty($_SESSION['login_session']))
        {
            $this->redirect(array('controller' => 'Admin', 'action' => 'login_index',));
        }

        if(!empty($this->data['admin_del']['admin_check']))
        {
            $this->__admin_del($this->data['admin_del']['admin_check']);
        }

        $this->__server_status();

        $admin_list = $this->tbl_admin_member->get_admin_list($_SESSION['login_session']['admin_id']);

        $this->set(compact('admin_list'));
    }

    public function admin_create()
    {
        if(empty($_SESSION['login_session']))
        {
            $this->redirect(array('controller' => 'Admin', 'action' => 'login_index',));
        }
        else if ($_SESSION['login_session']['admin_auth'] != 0 )
        {
            $this->redirect(array('controller' => 'Notifi', 'action' => 'all'));
        }

        if(!empty($this->data))
        {
            $new_admin_info = $this->data['tbl_admin_member'];

            if(!empty($new_admin_info['admin_belong']))
            {
                if(!$this->tbl_admin_member->admin_id_check($new_admin_info['admin_id']))
                {
                    if($new_admin_info['admin_pass'] == $new_admin_info['admin_pass_conf'])
                    {
                        $new_admin_info['admin_auth'] = 1 ;
                        $new_admin_info['admin_pass'] = md5($new_admin_info['admin_pass']);
                        unset($new_admin_info['admin_pass_conf']);

                        $this->tbl_admin_member->create();
                        if($this->tbl_admin_member->save($new_admin_info))
                        {
                            /*$log_data = connectionLog(
                                ADMIN_ADD,
                                $_SESSION['login_session']['admin_id'],
                                $_SESSION['login_session']['admin_name'],
                                '',
                                $new_admin_info['admin_id'],
                                $this->request->clientIp()
                            );

                            $this->connection_log->create();
                            $this->connection_log->save($log_data);*/
                            //로그 설정
                            $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>'Admin_Create','operation_data'=>$new_admin_info,'operation_result'=>'Login Success');
                            $this->tbl_log->save($logdata);

                            $this->redirect(array('controller' => 'Admin', 'action' => 'admin_list',));
                        }
                    }
                    else
                    {
                        $admin_error['pass'] = __(LANG_ADMIN_PASS_CHECK);
                    }
                }
                else
                {
                    $admin_error['id'] = __(LANG_ADMIN_SAME_ID);
                }
            }
            else
            {
                $admin_error['empty'] = __(LANG_ADMIN_EMPTY);
            }
        }

        $this->set(compact('admin_info', 'admin_error', 'new_admin_info'));
    }

    public function admin_edit($_id)
    {
        if(empty($_SESSION['login_session']))
        {
            $this->redirect(array('controller' => 'Admin', 'action' => 'login_index',));
        }

        if(!empty($this->data))
        {
            $new_admin_info = $this->data;

            $admin_info = $this->tbl_admin_member->get_admin_by_id($_id);

            if(!empty($new_admin_info['admin_member']['admin_pass']) &&
                !empty($new_admin_info['admin_member']['admin_new_pass']) &&
                !empty($new_admin_info['admin_member']['admin_pass_conf']) &&
                !empty($new_admin_info['admin_member']['admin_name']))
            {
                if(md5($new_admin_info['admin_member']['admin_pass']) == $admin_info['tbl_admin_member']['admin_pass'])
                {
                    if($new_admin_info['admin_member']['admin_new_pass'] == $new_admin_info['admin_member']['admin_pass_conf'])
                    {
                        $new_admin_id['admin_id'] = $_id;
                        $new_admin_data['admin_auth'] = $admin_info['tbl_admin_member']['admin_auth'];
                        $new_admin_data['admin_pass'] = md5($new_admin_info['admin_member']['admin_new_pass']);
                        $new_admin_data['admin_belong'] =$new_admin_info['admin_member']['admin_belong'];
                        $new_admin_data['admin_name'] = $new_admin_info['admin_member']['admin_name'];

                        if($this->tbl_admin_member->updateAll($new_admin_data, $new_admin_id))
                        {
                            /*$log_data = connectionLog(
                                ADMIN_EDIT,
                                $_SESSION['login_session']['admin_id'],
                                $_SESSION['login_session']['admin_name'],
                                '',
                                '',
                                $this->request->clientIp()
                            );

                            $this->connection_log->create();
                            $this->connection_log->save($log_data);*/
                            //로그 설정
                            $logdata = array("client_ip"=>$_SERVER['REMOTE_ADDR'],"date:"=>date('Y-m-d H:i:s',time()),'operation_info'=>'Admin_Edit','operation_data'=>$new_admin_info,'operation_result'=>'Login Success');
                            $this->tbl_log->save($logdata);

                            $this->redirect(array('controller' => 'Admin', 'action' => 'admin_list',));
                        }
                    }
                    else
                    {
                        $admin_error['new_pass'] = __(LANG_ADMIN_PASS_CHECK_NEW);
                    }
                }
                else
                {
                    $admin_error['pass'] = __(LANG_ADMIN_PASS_CHECK);
                }
            }
            else
            {
                $admin_error['empty'] = __(LANG_ADMIN_EMPTY);
            }
        }

        $admin_info = $this->tbl_admin_member->get_admin_by_id($_id);

        $this->set(compact('admin_info', 'admin_error', 'new_admin_info'));
    }

    private function __server_status()
    {
        App::uses('HttpSocket', 'Network/Http');
        $HttpSocket = new HttpSocket();

        $url = $this->server_url . '/web_server_status';
        $result = json_decode($HttpSocket->post($url), true);

        if( empty($result) ) {
            $this->Session->setFlash('서버의 응답이 없습니다.', 'default', array('class' => 'message'), 'bad');
            return;
        }

        if( $result['rcode'] != 0 ) {
            $this->Session->setFlash('에러 코드 : '.$result['rcode'], 'default', array('class' => 'message'), 'bad');
            return;
        }

        $server_status = $result['data']['web_server'];
        $this->set(compact('server_status'));
    }

    public function server_on()
    {
        App::uses('HttpSocket', 'Network/Http');
        $HttpSocket = new HttpSocket();

        if($this->passedArgs['server'] == 0)
        {
            $url = $this->server_url . '/web_server_on_user';
            $result = json_decode($HttpSocket->post($url), true);
        }
        else
        {
            $url = $this->server_url . '/web_server_on_dev';
            $result = json_decode($HttpSocket->post($url), true);
        }

        if( empty($result) ) {
            $this->Session->setFlash('서버의 응답이 없습니다.', 'default', array('class' => 'message'), 'bad');
            return;
        }

        if( $result['rcode'] != 0 ) {
            $this->Session->setFlash('에러 코드 : '.$result['rcode'], 'default', array('class' => 'message'), 'bad');
            return;
        }

        $this->redirect(array('action' => 'admin_list'));

        /*if($result['data']['web_server'] == 0)
            $server_status = 'Server ON';
        else
            $server_status = 'Server OFF';

        $this->set(compact('server_status'));*/
    }

    public function user_char_total()
    {
        $user_list = array();
        $char_uc_count = 0 ;
        $char_360_count = 0 ;
        $char_baidu_count = 0 ;
        //$char_mzw_count = 0 ;
        //$char_renren_count = 0 ;

        $user_list['CN_UC'] = $this->tbl_account->get_nstore_user_count();
        $user_list['CN_360'] = $this->tbl_account->get_google_user_count();
        $user_list['CN_BAIDU'] = $this->tbl_account->get_facebook_user_count();
        //$user_list['CN_MZW'] = $this->tbl_account->get_mzw_user_count();
        //$user_list['CN_RENREN'] = $this->tbl_account->get_renren_user_count();

        $user_list_uc = $this->tbl_account->get_nstore_user();

        foreach($user_list_uc as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_uc_count++ ;
        }

        $user_list_360 = $this->tbl_account->get_google_user();

        foreach($user_list_360 as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_360_count++ ;
        }

        $user_list_baidu = $this->tbl_account->get_facebook_user();

        foreach($user_list_baidu as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_baidu_count++ ;
        }

        /*$user_list_mzw = $this->tbl_account->get_mzw_user();

        foreach($user_list_mzw as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_mzw_count++ ;
        }

        $user_list_renren = $this->tbl_account->get_renren_user();

        foreach($user_list_renren as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_renren_count++ ;
        }*/



        $user_list['CN_UC_CHAR'] = $char_uc_count ;
        $user_list['CN_360_CHAR'] = $char_360_count ;
        $user_list['CN_BAIDU_CHAR'] = $char_baidu_count ;
        //$user_list['CN_MZW_CHAR'] = $char_mzw_count ;
        //$user_list['CN_RENREN_CHAR'] = $char_renren_count ;

        $this->set(compact('user_list'));
    }

    public function user_char_date_total($FGT_START_DATE)
    {
        $user_list = array();
        $char_uc_count = 0 ;
        $char_360_count = 0 ;
        $char_baidu_count = 0 ;
        $char_mzw_count = 0 ;
        $char_renren_count = 0 ;

        $start_date_str = sprintf("2015-07-%02d 00:00:00", $FGT_START_DATE);
        $end_date_str = sprintf("2015-07-%02d 23:59:59", $FGT_START_DATE);
        $start_date = new MongoDate(strtotime($start_date_str));
        $end_date = new MongoDate(strtotime($end_date_str));

        $user_list['CN_UC'] = $this->tbl_account->get_nstore_user_date_count($start_date,$end_date);
        $user_list['CN_360'] = $this->tbl_account->get_google_user_date_count($start_date,$end_date);
        $user_list['CN_BAIDU'] = $this->tbl_account->get_facebook_user_date_count($start_date,$end_date);
        //$user_list['CN_MZW'] = $this->tbl_account->get_mzw_user_date_count($start_date,$end_date);
        //$user_list['CN_RENREN'] = $this->tbl_account->get_renren_user_date_count($start_date,$end_date);

        $user_list_uc = $this->tbl_account->get_nstore_date_user($start_date,$end_date);

        foreach($user_list_uc as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_uc_count++ ;
        }

        $user_list_360 = $this->tbl_account->get_google_date_user($start_date,$end_date);

        foreach($user_list_360 as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_360_count++ ;
        }

        $user_list_baidu = $this->tbl_account->get_facebook_date_user($start_date,$end_date);

        foreach($user_list_baidu as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_baidu_count++ ;
        }

        /*$user_list_mzw = $this->tbl_account->get_mzw_date_user($start_date,$end_date);

        foreach($user_list_mzw as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_mzw_count++ ;
        }

        $user_list_renren = $this->tbl_account->get_renren_date_user($start_date,$end_date);

        foreach($user_list_renren as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null)
                $char_renren_count++ ;
        }*/

        $user_list['CN_UC_CHAR'] = $char_uc_count ;
        $user_list['CN_360_CHAR'] = $char_360_count ;
        $user_list['CN_BAIDU_CHAR'] = $char_baidu_count ;
        //$user_list['CN_MZW_CHAR'] = $char_mzw_count ;
        //$user_list['CN_RENREN_CHAR'] = $char_renren_count ;

        //DAU 계산

        $char_uc_dau_count = 0 ;
        $char_360_dau_count = 0 ;
        $char_baidu_dau_count = 0 ;
        $char_mzw_dau_count = 0 ;
        $char_renren_dau_count = 0 ;

        $start_date_dau = new MongoDate(strtotime("2015-07-09 00:00:00"));
        $user_dau_uc = $this->tbl_account->get_nstore_date_user($start_date_dau,$end_date);
        foreach($user_dau_uc as $num =>$user_value )
        {
            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$start_date,$end_date) != null)
                $char_uc_dau_count++;
        }

        $user_dau_360 = $this->tbl_account->get_google_date_user($start_date_dau,$end_date);
        foreach($user_dau_360 as $num =>$user_value )
        {
            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$start_date,$end_date) != null)
                $char_360_dau_count++ ;
        }

        $user_dau_baidu = $this->tbl_account->get_facebook_date_user($start_date_dau,$end_date);
        foreach($user_dau_baidu as $num =>$user_value )
        {
            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$start_date,$end_date) != null)
                $char_baidu_dau_count++ ;
        }

        /*$user_dau_mzw = $this->tbl_account->get_mzw_date_user($start_date_dau,$end_date);
        foreach($user_dau_mzw as $num =>$user_value )
        {
            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$start_date,$end_date) != null)
                $char_mzw_dau_count++ ;
        }

        $user_dau_renren = $this->tbl_account->get_renren_date_user($start_date_dau,$end_date);
        foreach($user_dau_renren as $num =>$user_value )
        {
            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$start_date,$end_date) != null)
                $char_renren_dau_count++ ;
        }*/

        $user_list['CN_UC_DAU'] = $char_uc_dau_count ;
        $user_list['CN_360_DAU'] = $char_360_dau_count ;
        $user_list['CN_BAIDU_DAU'] = $char_baidu_dau_count ;
        //$user_list['CN_MZW_DAU'] = $char_mzw_dau_count ;
        //$user_list['CN_RENREN_DAU'] = $char_renren_dau_count ;

        $this->set(compact('user_list','start_date_str','end_date_str'));
    }

    public function user_retention($FGT_START_DATE)
    {
        $user_list = array();

        $fgt_day = $FGT_START_DATE ;

        $char_uc_count = 0 ;
        $char_360_count = 0 ;
        $char_baidu_count = 0 ;
        $char_mzw_count = 0 ;
        $char_renren_count = 0 ;

        $start_date_str = sprintf("2015-07-%02d 00:00:00", $FGT_START_DATE);
        $end_date_str = sprintf("2015-07-%02d 23:59:59", $FGT_START_DATE);
        $start_date = new MongoDate(strtotime($start_date_str));
        $end_date = new MongoDate(strtotime($end_date_str));

        $retention_start_date_str = sprintf("2015-07-%02d 00:00:00", $FGT_START_DATE+1);
        $retention_end_date_str = sprintf("2015-07-%02d 23:59:59", $FGT_START_DATE+1);
        $retention_start_date = new MongoDate(strtotime($retention_start_date_str));
        $retention_end_date = new MongoDate(strtotime($retention_end_date_str));

        /*$char_uc_count = $this->tbl_account->get_nstore_user_date_count($start_date,$end_date);
        $char_360_count = $this->tbl_account->get_google_user_date_count($start_date,$end_date);
        $char_baidu_count = $this->tbl_account->get_facebook_user_date_count($start_date,$end_date);
        $char_mzw_count = $this->tbl_account->get_mzw_user_date_count($start_date,$end_date);
        $char_renren_count = $this->tbl_account->get_renren_user_date_count($start_date,$end_date);*/

        $char_uc_retention_count = 0 ;
        $char_360_retention_count = 0 ;
        $char_baidu_retention_count = 0 ;
        //$char_mzw_retention_count = 0 ;
        //$char_renren_retention_count = 0 ;

        $user_retention_uc = $this->tbl_account->get_nstore_date_user($start_date,$end_date);
        foreach($user_retention_uc as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_uc_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_uc_retention_count++;
        }

        $user_retention_360 = $this->tbl_account->get_google_date_user($start_date,$end_date);
        foreach($user_retention_360 as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_360_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_360_retention_count++;
        }

        $user_retention_baidu = $this->tbl_account->get_facebook_date_user($start_date,$end_date);
        foreach($user_retention_baidu as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_baidu_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_baidu_retention_count++;
        }

        /*$user_retention_mzw = $this->tbl_account->get_mzw_date_user($start_date,$end_date);
        foreach($user_retention_mzw as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_mzw_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_mzw_retention_count++;
        }

        $user_retention_renren = $this->tbl_account->get_renren_date_user($start_date,$end_date);
        foreach($user_retention_renren as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_renren_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_renren_retention_count++;
        }*/

        $user_list['CN_UC_CHAR'] = $char_uc_count ;
        $user_list['CN_360_CHAR'] = $char_360_count ;
        $user_list['CN_BAIDU_CHAR'] = $char_baidu_count ;
        //$user_list['CN_MZW_CHAR'] = $char_mzw_count ;
        //$user_list['CN_RENREN_CHAR'] = $char_renren_count ;

        $user_list['CN_UC_RETENTION'] = $char_uc_retention_count;
        $user_list['CN_360_RETENTION'] = $char_360_retention_count ;
        $user_list['CN_BAIDU_RETENTION'] = $char_baidu_retention_count ;
        //$user_list['CN_MZW_RETENTION'] = $char_mzw_retention_count ;
        //$user_list['CN_RENREN_RETENTION'] = $char_renren_retention_count ;

        $user_list['CN_TOTAL_CHAR'] = $char_uc_count + $char_360_count + $char_baidu_count;
        $user_list['CN_TOTAL_RETENTION'] = $char_uc_retention_count + $char_360_retention_count + $char_baidu_retention_count ;

        $this->set(compact('user_list','start_date_str','end_date_str','fgt_day'));
    }

    public function user_retention_7($FGT_START_DATE)
    {
        $user_list = array();

        $fgt_day = $FGT_START_DATE ;

        $char_uc_count = 0 ;
        $char_360_count = 0 ;
        $char_baidu_count = 0 ;
        $char_mzw_count = 0 ;
        $char_renren_count = 0 ;

        $start_date_str = sprintf("2015-07-%02d 00:00:00", $FGT_START_DATE);
        $end_date_str = sprintf("2015-07-%02d 23:59:59", $FGT_START_DATE);
        $start_date = new MongoDate(strtotime($start_date_str));
        $end_date = new MongoDate(strtotime($end_date_str));

        $retention_start_date_str = sprintf("2015-07-%02d 00:00:00", $FGT_START_DATE+7);
        $retention_end_date_str = sprintf("2015-07-%02d 23:59:59", $FGT_START_DATE+7);
        $retention_start_date = new MongoDate(strtotime($retention_start_date_str));
        $retention_end_date = new MongoDate(strtotime($retention_end_date_str));

        /*$char_uc_count = $this->tbl_account->get_nstore_user_date_count($start_date,$end_date);
        $char_360_count = $this->tbl_account->get_google_user_date_count($start_date,$end_date);
        $char_baidu_count = $this->tbl_account->get_facebook_user_date_count($start_date,$end_date);
        $char_mzw_count = $this->tbl_account->get_mzw_user_date_count($start_date,$end_date);
        $char_renren_count = $this->tbl_account->get_renren_user_date_count($start_date,$end_date);*/

        $char_uc_retention_count = 0 ;
        $char_360_retention_count = 0 ;
        $char_baidu_retention_count = 0 ;
        //$char_mzw_retention_count = 0 ;
        //$char_renren_retention_count = 0 ;

        $user_retention_uc = $this->tbl_account->get_nstore_date_user($start_date,$end_date);
        foreach($user_retention_uc as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_uc_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_uc_retention_count++;
        }

        $user_retention_360 = $this->tbl_account->get_google_date_user($start_date,$end_date);
        foreach($user_retention_360 as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_360_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_360_retention_count++;
        }

        $user_retention_baidu = $this->tbl_account->get_facebook_date_user($start_date,$end_date);
        foreach($user_retention_baidu as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_baidu_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_baidu_retention_count++;
        }

        /*$user_retention_mzw = $this->tbl_account->get_mzw_date_user($start_date,$end_date);
        foreach($user_retention_mzw as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_mzw_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_mzw_retention_count++;
        }

        $user_retention_renren = $this->tbl_account->get_renren_date_user($start_date,$end_date);
        foreach($user_retention_renren as $num =>$user_value )
        {
            if ( $this->tbl_character->get_character($user_value['tbl_account']['_id']) != null&&
                $user_value['tbl_account']['play_time']>0)
                $char_renren_count++ ;

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) != null)
                $char_renren_retention_count++;
        }*/

        $user_list['CN_UC_CHAR'] = $char_uc_count ;
        $user_list['CN_360_CHAR'] = $char_360_count ;
        $user_list['CN_BAIDU_CHAR'] = $char_baidu_count ;
        //$user_list['CN_MZW_CHAR'] = $char_mzw_count ;
        //$user_list['CN_RENREN_CHAR'] = $char_renren_count ;

        $user_list['CN_UC_RETENTION'] = $char_uc_retention_count;
        $user_list['CN_360_RETENTION'] = $char_360_retention_count ;
        $user_list['CN_BAIDU_RETENTION'] = $char_baidu_retention_count ;
        //$user_list['CN_MZW_RETENTION'] = $char_mzw_retention_count ;
        //$user_list['CN_RENREN_RETENTION'] = $char_renren_retention_count ;

        $user_list['CN_TOTAL_CHAR'] = $char_uc_count + $char_360_count + $char_baidu_count ;
        $user_list['CN_TOTAL_RETENTION'] = $char_uc_retention_count + $char_360_retention_count + $char_baidu_retention_count ;

        $this->set(compact('user_list','start_date_str','end_date_str','fgt_day'));
    }

    public function downloadLossUserdata($PLATFORM,$FGT_START_DATE)
    {
        $objPHPExcel = new PHPExcel();

        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
            ->setLastModifiedBy("Maarten Balliauw")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");


        $start_date_str = sprintf("2015-03-%02d 00:00:00", $FGT_START_DATE);
        $end_date_str = sprintf("2015-03-%02d 23:59:59", $FGT_START_DATE);
        $start_date = new MongoDate(strtotime($start_date_str));
        $end_date = new MongoDate(strtotime($end_date_str));

        $retention_start_date_str = sprintf("2015-03-%02d 00:00:00", $FGT_START_DATE+1);
        $retention_end_date_str = sprintf("2015-03-%02d 23:59:59", $FGT_START_DATE+1);
        $retention_start_date = new MongoDate(strtotime($retention_start_date_str));
        $retention_end_date = new MongoDate(strtotime($retention_end_date_str));

        //echo $PLATFORM. '   '.$FGT_START_DATE;
        //echo '<br />';

        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A' . 1, 'account')
            ->setCellValue('B' . 1, 'platform')
            ->setCellValue('C' . 1, 'nick_name')
            ->setCellValue('D' . 1, 'level')
            ->setCellValue('E' . 1, 'account created time')
            ->setCellValue('F' . 1, 'play_time')
            ->setCellValue('G' . 1, 'system_memory_size')
            ->setCellValue('H' . 1, 'device_model')
            ->setCellValue('I' . 1, 'os_info')
            ->setCellValue('J' . 1, 'connect_ip')
            ->setCellValue('K' . 1, 'stage_clear_infos');

        $user_retention_platform = $this->tbl_account->get_platform_date_user($PLATFORM,$start_date,$end_date);
        $num = 2 ;
        foreach($user_retention_platform as $user_value )
        {
            //print_r($user_value);
            //echo '<br />';

            if ( $this->tbl_connect_log->get_connection_log($user_value['tbl_account']['_id'],$retention_start_date,$retention_end_date) == null)
            {
                //print_r($user_value);
                //echo '<br />';
                $char_data = $this->tbl_character->get_character($user_value['tbl_account']['_id']);
                $device_data = $this->tbl_device_log->get_device_log($user_value['tbl_account']['_id']);

                if ($char_data != null) {
                    if ($user_value['tbl_account']['account'] == 'CN_UC_1000004344')
                    {
                        $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('A' . $num, $user_value['tbl_account']['account'])
                            ->setCellValue('B' . $num, $user_value['tbl_account']['platform'])
                            ->setCellValue('C' . $num, 'abcd')
                            ->setCellValue('D' . $num, $char_data[0]['tbl_character']['level'])
                            ->setCellValue('E' . $num, mongoDateToTime($user_value['tbl_account']['created']))
                            ->setCellValue('F' . $num, $user_value['tbl_account']['play_time'])
                            ->setCellValue('G' . $num, $device_data['tbl_device_log']['system_memory_size'])
                            ->setCellValue('H' . $num, $device_data['tbl_device_log']['device_model'])
                            ->setCellValue('I' . $num, $device_data['tbl_device_log']['os_full'])
                            ->setCellValue('J' . $num, $device_data['tbl_device_log']['connect_ip']);
                    }
                    else
                    $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A' . $num, $user_value['tbl_account']['account'])
                        ->setCellValue('B' . $num, $user_value['tbl_account']['platform'])
                        ->setCellValue('C' . $num, $char_data[0]['tbl_character']['nick_name'])
                        ->setCellValue('D' . $num, $char_data[0]['tbl_character']['level'])
                        ->setCellValue('E' . $num, mongoDateToTime($user_value['tbl_account']['created']))
                        ->setCellValue('F' . $num, $user_value['tbl_account']['play_time'])
                        ->setCellValue('G' . $num, $device_data['tbl_device_log']['system_memory_size'])
                        ->setCellValue('H' . $num, $device_data['tbl_device_log']['device_model'])
                        ->setCellValue('I' . $num, $device_data['tbl_device_log']['os_full'])
                        ->setCellValue('J' . $num, $device_data['tbl_device_log']['connect_ip']);

                $stage_data = $this->tbl_stage_log->get_stage_log($char_data[0]['tbl_character']['_id']);

                if ($stage_data != null) {
                    $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('K' . $num, $stage_data['tbl_stage_log']['stage_clear']);
                    }
                    else{
                        $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('K' . $num, 'none');
                    }

                    $num++ ;
                }
            }
        }

        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('LossUserList');

        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);


        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="LossUserList.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

    public function user_char_search()
    {
        $user_total_data = array();
        $user_list_db = $this->tbl_account->get_all_user();

        $objPHPExcel = new PHPExcel();

        //print_r($user_value);


        // Set document properties
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
            ->setLastModifiedBy("Maarten Balliauw")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");

        $user_value =  array() ;
        foreach($user_list_db as $num =>$user_value )
        {
            $char_data = $this->tbl_character->get_character($user_value['tbl_account']['_id']);

            //print_r($char_data);
            //echo '<br />';

            if ($char_data != null) {
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A' . $num, $user_value['tbl_account']['account'])
                    ->setCellValue('B' . $num, $user_value['tbl_account']['platform'])
                    ->setCellValue('C' . $num, $char_data[0]['tbl_character']['nick_name'])
                    ->setCellValue('D' . $num, $char_data[0]['tbl_character']['level'])
                    ->setCellValue('E' . $num, $char_data[0]['tbl_character']['char_class'])
                    ->setCellValue('F' . $num, mongoDateToTime($user_value['tbl_account']['created']))
                    ->setCellValue('G' . $num, $char_data[0]['tbl_character']['gender'])
                    ->setCellValue('H' . $num, $user_value['tbl_account']['play_time'])
                    ->setCellValue('I' . $num, $user_value['tbl_account']['crystal']);
            }
            else{
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A' . $num, $user_value['tbl_account']['account'])
                    ->setCellValue('B' . $num, $user_value['tbl_account']['platform'])
                    ->setCellValue('C' . $num, "null")
                    ->setCellValue('D' . $num, "null")
                    ->setCellValue('E' . $num, "null")
                    ->setCellValue('F' . $num, mongoDateToTime($user_value['tbl_account']['created']))
                    ->setCellValue('H' . $num, $user_value['tbl_account']['play_time'])
                    ->setCellValue('I' . $num, $user_value['tbl_account']['crystal']);
            }
        }

        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('UserList');

        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);


        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="UserList.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

    function mongoDateToTime($_mongo_date, $_flag = true)
    {
        $time = 0;
        foreach($_mongo_date as $key => $value)
        {
            if($key == 'sec')
            {
                $time = $value;
                break;
            }
        }

        if($_flag == true)
        {
            return mkTimeToDate($time);
        }
        else
        {
            return $time;
        }

    }

    function mkTimeToDate($_mongo_date)
    {
        $time = date('Y-m-d H:i:s', $_mongo_date);
        return $time;
    }
}